<?php

/* If no headers was sent: main call */
if (!headers_sent())
{
    $excel = True;

    require('database.php');
    require('auth.php');
    require('core.php');

    // Verifica se esta autenticado com pelo menos nivel 3
    validaPermissao(3);

    // Define file type
    header("Content-type: application/vnd.ms-excel");

    // Define file name and to be downloaded
    header("Content-Disposition: attachment; filename=\"Customers.xls\"");
    
}

$resultado = query_bd("SELECT *, customers.id as custid FROM customers, contacts WHERE customers.contact_id=contacts.id ORDER BY contacts.name");

// Mostra uma tabela com todos os clientes e alguns dados
echo "<table id=customers>";
if (!isset($excel))
    echo "<caption><em>", mysql_affected_rows(), " Customers</em></caption>";
echo "<tr><th>Name<th>Contact";

while ($customer = mysql_fetch_array($resultado))
{
	flush();	/* Manda o que ja tem em buffer para o cliente */

	echo "<tr><td><a href='"  . dirname($_SERVER['HTTP_REFERER']) . "/customer.php?id={$customer['custid']}'>" . 
         $customer['name'] . "</a>";
	echo "<td>{$customer['number']} {$customer['street']} {$customer['complement']} ",
		"{$customer['city']} {$customer['state']} {$customer['zipcode']} ",
		"{$customer['phone']} {$customer['cellphone']}<br>{$customer['email']}";

	unset($customer);
}
unset($resultado);

echo "</table>";

?>
